var dump=function(o){
    console.log(o);
};
var ongetToken=false;
var onupload=false;
var actover=function(li){
    this.show=function(){
        ttk_tools.removeClass(li,'ttk-main-panel-waterfall-over-hide');
    };
    this.hide=function(){
        ttk_tools.addClass(li,'ttk-main-panel-waterfall-over-hide');
    }
};
var up_img=function(element,li,over,imgsrc,u){
    this.getToken=function(){
        //TO DO  通过贴图库API上传
    };
    this.upload=function(){
        var url='http://web.tietuku.com/Pic/bookmarklet?link='+encodeURIComponent(imgsrc);
        vReturnValue = window.showModalDialog(url);
        // ttk_tools.ajax(url,imgsrc,function(){ 
        //         element.innerHTML='上传中';
        //         li.onmouseout=null; 
        //         element.onclick=null;
        //     },function(str){
        //         var obj=eval ("(" + str + ")");
        //         if(obj.state=='SUCCESS'){
        //             element.innerHTML='打开';
        //             ttk_tools.addClass(element,'ttk-success');
        //             var u='http://web.tietuku.com/Pic/go/mid/'+obj.title;
        //             var up=new up_img(null,null,null,null,u)
        //             element.onclick=up.open;
        //         }else{
        //             var c=new actover(over);
        //             li.onmouseout=c.hide; 
        //             element.innerHTML='上传';
        //             ttk_tools.removeClass(element,'ttk-success');
        //             var up=new up_img(element,li,over,imgsrc,null)
        //             element.onclick=up.upload;
        //             alert('上传失败!');
        //         }
        //     },function(){ 
        //         var c=new actover(over);
        //         li.onmouseout=c.hide; 
        //         element.innerHTML='上传';
        //         ttk_tools.removeClass(element,'ttk-success');
        //         var up=new up_img(element,li,over,imgsrc,null)
        //         element.onclick=up.upload;
        //         alert('上传失败!');  
        //     });
    };
    this.open=function(){
         window.open(u);
    }
};
var ttk_tools={
    'hasClass':function(element, className) { 
        var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); 
        return element.className.match(reg); 
    },
    'addClass':function(element, className) { 
        if (!this.hasClass(element, className)) { 
            element.className += " "+className; 
        } 
    },
    'removeClass':function(element, className) { 
        if (this.hasClass(element, className)) { 
            var reg = new RegExp('(\\s|^)'+className+'(\\s|$)'); 
            element.className = element.className.replace(reg,' '); 
        } 
    },
    'loadcss':function(path,id){
        if(!path || path.length === 0){
            throw new Error('argument "path" is required !');
        }
        var head = document.getElementsByTagName('head')[0];
        var link = document.createElement('link');
        link.href = path+'?'+this.random(10000,99999);
        link.id = id;
        link.rel = 'stylesheet';
        link.type = 'text/css';
        head.appendChild(link);
    },
    'loadjs':function(path){
        if(!path || path.length === 0){
            throw new Error('argument "path" is required !');
        }
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.src = path;
        script.type = 'text/javascript';
        head.appendChild(script);
    },
    'addEvent':function(oTarget,sEventType,funName){
        if(oTarget.addEventListener){
            oTarget.addEventListener(sEventType,funName, false);
        }else if(oTarget.attachEvent){
            oTarget.attachEvent("on"+sEventType,funName);
        }else{
            oTarget["on"+sEventType]=funName;
        }
    },
    'removeEvent':function(oTarget,sEventType,funName){
        if(oTarget.removeEventListener){
            oTarget.removeEventListener(sEventType,funName, false);
        }else if(oTarget.detachEvent){
            oTarget.detachEvent("on"+sEventType,funName);
        }else{
            oTarget["on"+sEventType]=null;
        }
    },
    'random':function(min,max){
        return Math.floor(min+Math.random()*(max-min));
    },
    'match_width':function(){
        return (document.body.clientWidth-200);
    },
    'ajax':function(url,linkurl,fnTip,fnSucc,FnFaild){ 
        var oAjax=null; 
        if(window.XMLHttpRequest){ 
            oAjax=new XMLHttpRequest(); 
        }else{ 
            oAjax=new ActiveXObject('Microsoft.XMLHTTP');    
        } 
        oAjax.open('POST',url,true); 
        oAjax.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8"); 
        oAjax.send('link='+linkurl); 
        if(fnTip)fnTip(); 
        oAjax.onreadystatechange=function(){ 
            if(oAjax.readyState==4){ 
                if(oAjax.status==200){ 
                    if(fnSucc){fnSucc(oAjax.responseText)}; 
                }else{ 
                    if(FnFaild)FnFaild();    
                } 
            }    
        }    
    },
    'isString':function(exp){
        var v=false;
        if (typeof(exp)=="string" && exp!='undefined'){
            v=true;
        }
        return v;
    },
    'setCookie':function(name,value) { 
        var Days = 30; 
        var exp = new Date(); 
        exp.setTime(exp.getTime() + Days*24*60*60*1000); 
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
    },
    'getCookie':function(name) { 
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg))
            return unescape(arr[2]); 
        else 
            return null; 
    },
    'delCookie':function(name) { 
        var exp = new Date(); 
        exp.setTime(exp.getTime() - 1); 
        var cval=getCookie(name); 
        if(cval!=null) 
            document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
    } 
};
var ttk_plus={
    'list':'',
    'loadImg':function(){
        var list=document.getElementsByTagName('img');
        var txt = '' ;
        for(var val in list){
            if(ttk_tools.isString(list[val].src)){
                var img = new Image();
                img.src = list[val].src;
                if(img.height>0)
                    txt+='{ "src":"'+list[val].src+'" , "alt":"'+list[val].alt+'" ,"width":"'+img.width+'" ,"height":"'+img.height+'" },';
                
            }
        }
        this.list=eval ("([" + txt + "])");
    },
    'Main':function(){
        this.loadImg();
        this.showPanel();
        ttk_tools.loadcss('http://git.oschina.net/ltteam/tietuku_plus_bookmark/raw/master/widgets.css','ttk_link');
        ttk_tools.addEvent(window,'resize',function() {ttk_plus.w_resize()})
        var close=document.getElementById('ttk-close');
        ttk_tools.addEvent(close,'click',function() {ttk_plus.destroy()})
        return null;
    },
    'showPanel':function(){
        var h=document.getElementsByTagName('html');
        ttk_tools.addClass(h[0],'ttk-hidden');
        var Panel = document.createElement('div');
        Panel.setAttribute('class','ttk-main-panel');
        Panel.setAttribute('id','ttk-main-panel');
        var header=document.createElement('div');
        header.setAttribute('class','ttk-main-panel-header');
        var inner=document.createElement('div');
        inner.setAttribute('class','ttk-main-panel-header-inner');
        inner.setAttribute('style','width:'+ttk_tools.match_width()+'px;');
        inner.setAttribute('id','ttk-main-inner');
        var logo=document.createElement('a');
        logo.setAttribute('class','ttk-main-panel-header-logo');
        logo.setAttribute('href','http://tietuku.com');
        logo.setAttribute('target','_blank');
        var logoimg=document.createElement('img');
        logoimg.setAttribute('src','http://huaban.com/img/widgets/logo.png');
        logo.appendChild(logoimg);
        inner.appendChild(logo);
        header.appendChild(inner);
        var close=document.createElement('div');
        close.setAttribute('class','ttk-main-panel-header-close');
        close.setAttribute('id','ttk-close');
        header.appendChild(close);
        Panel.appendChild(header);
        var holder = document.createElement('div');
        holder.setAttribute('class','ttk-main-panel-waterfall-holder');
        var waterfall = document.createElement('div');
        waterfall.setAttribute('class','ttk-main-panel-waterfall');
        waterfall.setAttribute('style','width:'+ttk_tools.match_width()+'px')
        waterfall.setAttribute('id','ttk-waterfall');
        var ul = document.createElement('ul');
        for(var val in this.list){
            if(this.list[val].src!=undefined){
                var img=document.createElement('img');
                var imgdiv=document.createElement('div');
                imgdiv.setAttribute('style','width:100%;height:100%');
                img.setAttribute('src',this.list[val].src);
                var width=236;
                var height=width/this.list[val].width*this.list[val].height;
                img.setAttribute('width',width);
                img.setAttribute('height',height);
                var li = document.createElement('li');
                li.setAttribute('style','height:'+height+'px');
                li.setAttribute('class','ttk-main-panel-waterfall-li');
                li.setAttribute('id','li'+val);
                li.setAttribute('img',this.list[val].src);
                var over=document.createElement('div');
                over.setAttribute('class','ttk-main-panel-waterfall-over ttk-main-panel-waterfall-over-hide');
                var btn=document.createElement('div');
                btn.setAttribute('class','ttk-main-panel-waterfall-over-btn');
                btn.innerHTML='上传';
                over.appendChild(btn);
                imgdiv.appendChild(img);
                li.appendChild(imgdiv);
                li.appendChild(over);
                ul.appendChild(li);
                var c=new actover(over);
                li.onmouseover=c.show;
                li.onmouseout=c.hide; 
                var up=new up_img(btn,li,over,this.list[val].src);
                btn.onclick=up.upload;
            }
        }
        waterfall.appendChild(ul);
        holder.appendChild(waterfall);
        Panel.appendChild(holder);
        document.body.appendChild(Panel);
    },
    'w_resize':function(){
        var inner=document.getElementById('ttk-main-inner');
        var waterfall=document.getElementById('ttk-waterfall');
        inner.style.cssText='width:'+ttk_tools.match_width()+'px;';
        waterfall.style.cssText='width:'+ttk_tools.match_width()+'px;';
    },
    'destroy':function(){
        var h=document.getElementsByTagName('html');
        ttk_tools.removeClass(h[0],'ttk-hidden');
        ttk_tools.removeEvent(window,'resize',function() {ttk_plus.w_resize()})
        var close=document.getElementById('ttk-close');
        ttk_tools.removeEvent(close,'click',function() {ttk_plus.destroy()})
        var body=document.getElementsByTagName('body');
        var head=document.getElementsByTagName('head');
        var Panel=document.getElementById('ttk-main-panel');
        body[0].removeChild(Panel);
        var link=document.getElementById('ttk_link');
        head[0].removeChild(link);
        var script=document.getElementById('ttk_script');
        body[0].removeChild(script);
    },
};
(function(){
    ttk_plus.Main();
})();







//javascript:(function(a,b){b=a.createElement('script'),b.setAttribute('charset','utf-8'),b.setAttribute('id','ttk_script'),b.src='http://web.tietuku.com/widgets.js?'+Math.floor(+new Date/1e7),a.body.appendChild(b)})(document)